package com.wangyadong.hobby.nio.netty1;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.ServerSocketChannel;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;

import java.net.InetSocketAddress;

public class ServerEcho {

    public static void main(String[] args) throws InterruptedException {
        ServerEchoHandler handler = new ServerEchoHandler();
        NioEventLoopGroup group = new NioEventLoopGroup();
        //NioEventLoopGroup
        ServerBootstrap bootstrap = new ServerBootstrap();
        try {
            bootstrap.group(group)
                    .channel(NioServerSocketChannel.class)
                    .localAddress(new InetSocketAddress(9999))
                    .childHandler(new ChannelInitializer<SocketChannel>() {
                        @Override
                        protected void initChannel(SocketChannel socketChannel) throws Exception {
                            socketChannel.pipeline().addLast(handler);
                        }
                    });
            //绑定服务器
            ChannelFuture sync = bootstrap.bind().sync();
//            sync.channel().closeFuture();
        } finally {
            group.shutdownGracefully().sync();
        }

    }
}
